define(function () {
    /**
     * data.point   默认坐标
     * data.city    城市
     * data.confirm 确认回调
     */
    return function (data) {
        var html = $(utils.getHtml('map', data)).appendTo('body');
        html.modal('show').on('hidden.bs.modal', function (e) {
            html.remove();
        });
        var defaultPoint = data.point || data.defaultPoint || {lng: 0, lat: 0};
        setTimeout(function () {
            var map = new BMap.Map("map-container");
            map.enableScrollWheelZoom();
            map.enableContinuousZoom();
            var point = new BMap.Point(defaultPoint.lng, defaultPoint.lat);    // 创建点坐标
            var marker = new BMap.Marker(point);
            var label = new window.BMap.Label('拖动坐标确定位置', {offset: new window.BMap.Size(-50, -42)});
            label.setStyle({
                color: '#ff0000',
                backgroundColor: '0',
                fontSize: "12px",
                border: "0",
                fontWeight: "bold"
            });
            marker.setLabel(label);
            map.addControl(new BMap.NavigationControl());
            map.centerAndZoom(point, 15);                     // 初始化地图,设置中心点坐标和地图级别。
            marker.enableDragging();
            map.addOverlay(marker);

            /**
             * 定位
             */
            if (!data.point) {
                if (window.navigator && window.navigator.geolocation) {
                    window.navigator.geolocation.getCurrentPosition(function (position) {
                        var point = {
                            lat: position.coords.latitude,
                            lng: position.coords.longitude
                        }
                        utils.gcj2bd(point);
                        map.centerAndZoom(point, 15);
                        marker.setPoint(map.getCenter());
                    }, function (err) {
                        console.log(err);
                    }, {
                        enableHighAccuracy: true,
                    });
                } else {
                    console.log("浏览器不支持html5来获取地理位置信息");
                }
            }

            var cityInput = $('input[name="city"]', html), addressInput = $('select[name="address"]', html);
            addressInput.select2({
                placeholder: "请输入地址",
                ajax: {
                    dataType: "json",
                    delay: 250,
                    transport: function (params, success) {
                        var def = $.Deferred();
                        var search = new BMap.LocalSearch(cityInput.val(), {
                            onSearchComplete: function (results) {
                                if (results && results.getNumPois()) {
                                    var options = [];
                                    for (var i = 0; i < results.getCurrentNumPois(); i++) {
                                        var poi = results.getPoi(i);

                                        console.log(poi);

                                        options.push({
                                            id: (poi.point.lat + ',' + poi.point.lng),
                                            text: poi.title + ' (' + poi.address + ')'
                                        });
                                    }
                                    return def.resolve(success({results: options}));
                                } else {
                                    return def.resolve(success({results: []}));
                                }
                            }
                        });
                        search.search(params.data.q);
                        return def;
                    }
                },
                language: 'zh-CN',
                //兼容fastclick
                templateResult: function (result, container) {
                    if (!result.id) {
                        return result.text;
                    }
                    container.className += ' needsclick';
                    return result.text;
                }
            }).on('select2:select', function (e) {
                var arr = $(this).val().split(',');
                var point = {
                    lat: arr[0],
                    lng: arr[1]
                }
                var text = $(this).text();
                var region = text.split(' (');
                marker.region = {
                    name: region[0],
                    address: region[1].substr(0, region[1].length - 1)
                }
                map.centerAndZoom(point, 18);
                marker.setPoint(map.getCenter());
            });
            //兼容fastclick
            addressInput.each(function (index, el) {
                $(el).data('select2').$container.find('*').addClass('needsclick');
            });

            $('.btn-confirm', html).click(function () {
                var point = marker.getPoint();
                data.confirm(point, marker.region);
                html.modal('hide');
            });
        }, 200);
    }
});
